package com.lngsyz.ywtb.biz.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.ywtb.entity.source1.SystemFileupload;
import com.lngsyz.ywtb.mapper.source1.SystemFileuploadMapper;
import com.lngsyz.ywtb.util.CommonUtil;
import com.lngsyz.ywtb.util.StringUtil;

@Service
public class SystemFileuploadService {
    
	@Autowired
	SystemFileuploadMapper systemFileuploadMapper;
	
    public SystemFileupload findById(String id){
    	SystemFileupload sf = systemFileuploadMapper.findById(id);
		return sf;
    }
    
    public void insert(SystemFileupload systemFileupload) {
    	if(systemFileupload != null) {
    		systemFileuploadMapper.insert(systemFileupload);
    	}
    }
    
    public SystemFileupload packageSystemFileupload(String district, String fileName) {
    	SystemFileupload rtn = new SystemFileupload();
		// 本地服务器文件绝对路径 D:/img/
		String serverPath = StringUtil.getConfiguration("config.properties", "IMAGE_URL");
		// 本地服务器文件相对路径 /img/
		String relativePath = StringUtil.getConfiguration("config.properties", "IMAGE_URL_RELATIVE");

		SimpleDateFormat formatDIR = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String dateDir = formatDIR.format(date) + "/";
		// 图片目录
		String applyType = "1000";
		String document = "permit/";
		// 文件服务器上的相对路径
		String relativelyPath = "";//
		String folderPath = "";//
		String filePath = "";//
		
		String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
		String imageName = CommonUtil.createUuid() + "." + fileSuffix;
		// 文件位置
		filePath = serverPath + document + dateDir + district + imageName;
		// 文件服务器上的相对路径
		relativelyPath = relativePath + document + dateDir + district + imageName;
		// 文件夹位置
		folderPath = serverPath + document + dateDir + district;
		// 创建文件夹
		CommonUtil.getMkdir(folderPath);
		
		rtn.setId(CommonUtil.createUuid());
		rtn.setApplytype(applyType);
		rtn.setAccessurl(relativelyPath);
		rtn.setFilesuffix(fileSuffix);
		rtn.setFilename(fileName);
		rtn.setFilepath(filePath);
		rtn.setCreatename(imageName);
		rtn.setUserCreate("");
//		rtn.setHeight(height);
//		rtn.setWidth(width);
//		rtn.setBitsperpixel(bitsperpixel);
//		rtn.setColortype(colortype);
//		rtn.setHeightinch(heightinch);
//		rtn.setWidthinch(widthinch);
//		rtn.setDpi(dpi);
//		rtn.setFilesize(f.length() + "");
    	return rtn;
    }
}
